package org.jet.emall.common.util;

/**
 * @author xiaozai
 * @version 1.0
 * @date 2020-04-09 13:30
 */
public class RegUtils {


    public static void main(String [] args){

        String raw = "/roles/{id}/status/{status}";
        String s = handle(raw);
        System.out.println("s=["+s+"]");

        String test = "/roles/1/status/1";
        System.out.println(test.matches(s));
        test = "/role/1/status/1";
        System.out.println(test.matches(s));
    }

    /**
     * 将restful风格的url中的 {参数}  替换成 \w+
     *
     * @param rawString 示例:"/roles/{id}/status/{status}"
     * @return 示例: "/roles/\w+/status/\w+"
     */
    public static String handle(String rawString){
        StringBuilder stringBuilder = new StringBuilder(rawString);
        int begin=0,end=0;
        for(int i=0;i<stringBuilder.length();i++){
            if(stringBuilder.charAt(i)=='{'){
                begin = i;
            }
            if(begin>0&&stringBuilder.charAt(i)=='}'){
                end = i+1;
                stringBuilder.replace(begin,end,"\\w+");
                i=begin+2;
                begin = 0 ;
            }
        }
        return stringBuilder.toString();
    }
}

